/*******************************************************************************
 * Copyright (c) 2013- <Mirko Cardoso>
 * Licence GPLv2
 * 
 * Contributors:
 * Mirko Cardoso 6-dic-2013
 *******************************************************************************/
package doscio.filereader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author doscio
 * 
 */
public class FileSystemReader {

	private String mainPath;

	private List<String> fileNameList;
	private String workingPath = "";

	public FileSystemReader(String mainPath) {
		this.mainPath = mainPath;
		fileNameList = new ArrayList<>();
	}

	public void readMainPath() {
		readPath(mainPath);

	}

	/**
	 * Read filename from folderPath
	 * 
	 * @param folderPath
	 *            path
	 */
	public void readPath(String folderPath) {

		
		// Add the '/' at the end of the path
		if (folderPath.charAt(folderPath.length() - 1) != ('/')) {
			workingPath = folderPath + "/";
		} else {
			workingPath = folderPath;
		}

		try {

			File file = new File(workingPath);
			String[] filesName = file.list();

			for (String name : filesName) {

				File f = new File(workingPath + name);
				if (!f.isDirectory()) {
					if (name.charAt(0) != '.')
						fileNameList.add(name);
				}

			}
		} catch (NullPointerException e) {

			System.err.println("Path not found");
			System.err.println("Path: " + workingPath);

			System.exit(-1);

		}

	}

	
	public void setMainPath(String mainPath){
		this.mainPath=mainPath;
	}
	
	
	public List<String> getFileList() {
		return fileNameList;
	}

}
